package off_transaction

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	// "bp/internal/util"
	"context"
)

// DisableOffTransactionOfferItemStatsByOutTransNo 根据外部交易号作废优惠交易
func (s *OffTransactionEntityServiceImp) DisableOffTransactionOfferItemStatsByOutTransNo(ctx context.Context, q *sqlc.Queries, outTransNo string) (err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = q.DisableOffTransactionOfferItemStatsByOutTransNo(ctx, outTransNo)
	if err != nil {
		return
	}
	return
}

// DisableOffTransactionOfferItemStatsByOutTransNoAndOutOrderNoParams 根据外部交易号+外部订单号
type DisableOffTransactionOfferItemStatsByOutTransNoAndOutOrderNoParams struct {
	OutTransNo string
	OutOrderNo string
}

// DisableOffTransactionByOutTransNoAndOutOrderNo 根据外部交易号+外部订单号作废优惠交易
func (s *OffTransactionEntityServiceImp) DisableOffTransactionOfferItemStatsByOutTransNoAndOutOrderNo(ctx context.Context, q *sqlc.Queries, ptr *DisableOffTransactionOfferItemStatsByOutTransNoAndOutOrderNoParams) (err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = q.DisableOffTransactionOfferItemStatsByOutTransNoAndOutOrderNo(ctx, sqlc.DisableOffTransactionOfferItemStatsByOutTransNoAndOutOrderNoParams{
		OutTransNo: ptr.OutTransNo,
		OutOrderNo: ptr.OutOrderNo,
	})
	if err != nil {
		return
	}
	return
}

type RefreshOffTransactionOfferItemStatsByOutTransNoAndOutOrderNoParam struct {
	OutTransNo                                  string
	OutOrderNo                                  string
	UpsertOffTransactionOfferItemStatParamsPtrs []*sqlc.UpsertOffTransactionOfferItemStatParams
}

// RefreshOffTransactionOfferItemStatsByOutTransNoAndOutOrderNo 优惠交易批量刷新优惠项目统计
func (s *OffTransactionEntityServiceImp) RefreshOffTransactionOfferItemStatsByOutTransNoAndOutOrderNo(ctx context.Context, q *sqlc.Queries, ptr *RefreshOffTransactionOfferItemStatsByOutTransNoAndOutOrderNoParam) (offTransactionOfferItemStatPtrs []*sqlc.OffTransactionOfferItemStat, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = s.DisableOffTransactionOfferItemStatsByOutTransNoAndOutOrderNo(ctx, q, &DisableOffTransactionOfferItemStatsByOutTransNoAndOutOrderNoParams{
		OutTransNo: ptr.OutTransNo,
		OutOrderNo: ptr.OutOrderNo,
	})
	if err != nil {
		return
	}
	for _, ptr := range ptr.UpsertOffTransactionOfferItemStatParamsPtrs {
		offTransactionOfferItemStat, upErr := q.UpsertOffTransactionOfferItemStat(ctx, *ptr)
		if upErr != nil {
			err = upErr
			return
		}
		offTransactionOfferItemStatPtrs = append(offTransactionOfferItemStatPtrs, &offTransactionOfferItemStat)
	}
	return
}
